package com.softserveinc.moviedbapp.manager;

import android.util.Log;

import com.softserveinc.moviedbapp.connection.GeneralException;
import com.softserveinc.moviedbapp.connection.InputStreamProvider;
import com.softserveinc.moviedbapp.connection.MovieDBConnectionException;
import com.softserveinc.moviedbapp.model.Movie;
import com.softserveinc.moviedbapp.model.MovieDetails;
import com.softserveinc.moviedbapp.parser.IMovieDBMethodBrowseSearch;
import com.softserveinc.moviedbapp.parser.IMovieDBMethodGetInfo;
import com.softserveinc.moviedbapp.parser.MethodBrowseSearchParserFactory;
import com.softserveinc.moviedbapp.parser.MethodGetInfoParserFactory;
import com.softserveinc.moviedbapp.util.MovieAppConstants;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class DownloadManager {

	private static String TAG = DownloadManager.class.getSimpleName();
	private List<Movie> mMovies;
	private MovieDetails mMovieInfo;

	/* download xml and create a list of movie objects */
	public List<Movie> getMovieList(final String language, final String type, final String orderBy, final String page,
			final String results) throws GeneralException {
		Log.d(TAG, "inside getMovieList() ");
		String order = "";
		if (orderBy.equals("title")) {
			order = MovieAppConstants.ORDER_ASC;
		} else {
			order = MovieAppConstants.ORDER_DESC;
		}

		String region = getRegion(language);

		String lang = correctLang(language);

		String apiUrl = MovieAppConstants.METHOD_BROWSE + lang + region + type + "/" + MovieAppConstants.API_KEY
				+ "?order_by=" + orderBy + "&order=" + order + "&genres=18&min_votes=10&page=" + page + "&per_page="
				+ results + "&release_min=978307199&release_max=1357060191";

		Log.d(TAG, "apiUrl " + apiUrl);

		IMovieDBMethodBrowseSearch parser = MethodBrowseSearchParserFactory.getParser(type);
		InputStreamProvider isp = new InputStreamProvider();
		int retry = 3;
		while (retry > 0) {
			InputStream is;
			try {
				is = isp.getInputStream(apiUrl);
			} catch (IOException e1) {
				Log.e(TAG, "Caught IOException.", e1);
				throw new GeneralException("There's no connection");
			}
			try {
				mMovies = parser.parse(is);
				break;
			} catch (MovieDBConnectionException e) {
				Log.e(TAG, "Caught MovieDBConnectionException. Going to re-try", e);
				retry--;
				continue;
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					Log.e(TAG, "TT", e);
				}
			}
		}
		return mMovies;
	}

	/* download xml and create movieInfo object */
	public MovieDetails getMovieInfo(final String language, final String type, final int movieId) throws GeneralException {
		Log.d(TAG, "inside getMovieInfo() ");

		String region = getRegion(language);

		String lang = correctLang(language);

		String apiUrl = MovieAppConstants.METHOD_GETINFO + lang + region + type + "/" + MovieAppConstants.API_KEY + "/"
				+ movieId;

		Log.d(TAG, "apiUrl " + apiUrl);

		IMovieDBMethodGetInfo parser = MethodGetInfoParserFactory.getParser(type);
		InputStreamProvider isp = new InputStreamProvider();
		int retry = 3;
		while (retry > 0) {
			InputStream is;
			try {
				is = isp.getInputStream(apiUrl);
			} catch (IOException e1) {
				Log.e(TAG, "Caught IOException.", e1);
				throw new GeneralException("There's no connection");
			}
			try {
				mMovieInfo = parser.parseMovieInfo(is);

				break;
			} catch (MovieDBConnectionException e) {
				Log.e(TAG, "Caught MovieDBConnectionException. Going to re-try", e);

				retry--;
				continue;
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					Log.e(TAG, "TT", e);
				}
			}
		}
		return mMovieInfo;
	}

	/* perform a search for a movie title */
	public List<Movie> searchForMovie(final String language, final String type, final String query)
			throws GeneralException {
		Log.d(TAG, "inside searchForMovie() ");

		String apiUrl = MovieAppConstants.METHOD_SEARCH + language + "/" + type + "/" + MovieAppConstants.API_KEY + "/"
				+ query;
		Log.d(TAG, "apiUrl " + apiUrl);

		IMovieDBMethodBrowseSearch parser = MethodBrowseSearchParserFactory.getParser(type);

		InputStreamProvider isp = new InputStreamProvider();
		int retry = 3;
		while (retry > 0) {
			InputStream is;
			try {
				is = isp.getInputStream(apiUrl);
			} catch (IOException e1) {
				Log.e(TAG, "Caught IOException.", e1);
				throw new GeneralException("There's no connection");
			}
			try {
				mMovies = parser.parse(is);
				return mMovies;
			} catch (MovieDBConnectionException e) {
				Log.e(TAG, "Caught MovieDBConnectionException. Going to re-try", e);
				retry--;
				continue;
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					Log.e(TAG, "TT", e);
				}
			}
		}
		return null;

	}

	private String getRegion(final String language) {
		String region = "";
		if (language.equals("en")) {
			region = MovieAppConstants.LANGUAGE_EN;
		} else if (language.equals("ru")) {
			region = MovieAppConstants.LANGUAGE_RU;
		} else if (language.equals("uk")) {
			// region = MovieAppConstants.LANGUAGE_UA;
			region = MovieAppConstants.LANGUAGE_RU;
		} else {
			region = "/";
		}
		return region;
	}

	private String correctLang(final String language) {
		String lang = "";
		if (language.equals("uk")) {
			lang = "ru";
		} else {
			lang = language;
		}
		return lang;
	}
}
